<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>

</body>
<script>
	// 利用字符重复出现的次数，编写一种方法，实现基本的字符串压缩功能。比如，字符串aabcccccaaa会变为a2b1c5a3
		function stringCompress(str){
			let result = []
			let current = null
			for(const item of str){
				if (!current){
					current = {
						char: item,
						count: 1
					}
				} else if (current.char === item){
					current.count++
				} else {
					result.push(current.char + current.count)
					current = {
						char: item,
						count: 1
					}
				}
			}
			result.push(current.char + current.count)
			return result.join('')
		}
		const str = 'aabcccccaaa'
		console.log(stringCompress(str))
</script>
</html>